<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
error_reporting(0);

//输出图片
function CxImg($src) {
	
	$src = config('base.img_dir').$src;
	return $src;
	
}
//类型TITLE
function CxCarTypeTitle($CT) {
	
	$types = config('base.types');
	$car_types = config('base.car_types');
	
	$CT['title'] = $types[$CT['type']].$car_types[$CT['car_type']];
	return $CT;
	
}
function Arr2Set($arr,$glue = ',') {
	return is_array($arr) ? implode($glue,$arr) : $arr;
}
/**
* 分支树显示无限分类
* @param $arr 需要处理的数组
* @param $key 相当于数组单元的id
* @param $fkey 相当于数组单元的父类id
* @param $num 从第几层查起，也就是要查层的父类id
* @return $list 返回的数组
*/
function Arr2Tree($arr,$num = 0,$key='id',$fkey = 'pid')
{ 
    $list = [];
    foreach($arr as $val){ 
        if($val[$fkey] == $num){ 
            $tmp = Arr2Tree($arr,$val[$key],$key,$fkey);
            $val['children'] = $tmp ?: [];
            $list[] = $val;
        }
    }
    return $list;
}
function Arr2Sibling($arr ,$level = 0, $pid = 0, $key = 'id', $fkey = 'pid'){
	
	$data = [];
	$level = $pid == 0 ? 0 : $level+1;  
    foreach($arr as $k => $v){
    	$v['level'] = $level;#级别
        if($v[$fkey] === $pid){
            $data[$v[$key]]=$v;
            $data = array_merge($data ,Arr2Sibling($arr, $level,$v[$key],$key,$fkey));
        }
    }
    return $data;
}
/**
* 判断字符串是否存在
* @param str $object 
* @param str $find
* @param Bool $isMulti
* 
* @return FLASE 或位置(1-r)
*/
function StrFind($object, $find, $isMulti = 0) {
	
	if(!$object) return false;
	
	if($isMulti) {
		$pos = strpbrk($object ,$find);
	} else {
		$pos = strpos($object, $find);
		$pos !== FALSE && $pos++;
		if ($pos == strlen($object)) return 'r';
	}
	
	return $pos;
}
/**
* 加密
* @return 密文
*/
function StrEncrypt($string, $expiry = 0,$operation = 'ENCODE', $key = '1mi6'){
	
	$ckey_length = 4;
//	$key = md5($key ? $key : UC_KEY);
	$key = md5($key);
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
	$string_length = strlen($string);

	$result = '';
	$box = range(0, 255);

	$rndkey = array();
	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($cryptkey[$i % $key_length]);
	}

	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE') {
		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
			return substr($result, 26);
		} else {
			return '';
		}
	} else {
		return $keyc.str_replace('=', '', base64_encode($result));
	} 
}
/**
* 解密
* @return 明文
*/
function StrDecrypt($string, $expiry = 0, $key = '1mi6'){
    return StrEncrypt($string,$expiry,'DECODE',$key);
}

/**
* 语言包加载JS.CSS插件 
*/
function TplWidgets($widgets = [], $type = 'css') {
	
	$files = [];
	$static = config('base.static_dir');
	if( $widgets ) {
		$config = config('widgets.widgets');
		foreach($widgets AS $w){
			isset($config[$w][$type]) && $file = $config[$w][$type];
			if($file){
				foreach(explode(',',$file) AS $f ) {
					$f = StrFind($f, '//') ? $f : $static.$f;
					$files[] = $type == 'css' ? "<link rel='stylesheet' href='{$f}' />": "<script src='{$f}'></script>";
				}
			}
		}
	}
	return implode( array_unique($files) );
	
}

/**
* 
* @param arr $list
* @param arr $selected [选中]
* @param str $textfield
* @param str $valuefield
* @param arr $children_options ['key'：子集键名；]
* @param str $textisval
* @param int $hiddenval [0:隐藏0 ；1:]
* 
* @return
*/
function TplOptions($list,$selected=[],$textfield = 'name', $valuefield = '', $textisval = 0, $hiddenval = 1) {
	
	$html = "";
	!is_array($selected) && $selected = explode(',',$selected);

	if($list) {
		!is_array($list) && $list = explode(',',$list);
		$newlist = $valuefield ? array_column($list,null,$valuefield) : $list;
		foreach ($newlist as $key => $val){
			
			$level = 0;
			$sign = "";
			if (is_array($val)) {
				$level = $val['level'];
				$val = $val[$textfield];
			}
			//
			$value = $textisval ? $val : $key;
			$plus = in_array($value,$selected) ? ' selected' : '';
			$text = $textisval || $hiddenval ? $val : "{$value}:{$val}";
			//下级前缀
			if($level) {
				$sign = str_repeat(' ├  ',$level);
			}
			
			$html = $html . "<option value='{$value}'{$plus}>{$sign}{$text}</option>";
			
		}
	}
	
	return $html;
}

function CorePwd($pwd,$salt = '') {
	return md5( md5($pwd).$salt );
}